package com.woniuxy.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.entity.User1;
import com.woniuxy.service.User1Service;

import com.woniuxy.utils.Result;

@Controller
@RequestMapping("/user1")
public class User1Controller {
	@Autowired
	private User1Service userService;
	
	@RequestMapping("/login1")
	@ResponseBody
	public Result<Object> login(User1 user) {
		
		System.out.println(user);
		Result<Object> result = new Result<>();
		//认证
		//1.获取subject对象
		Subject currentUser = SecurityUtils.getSubject();
		//2.判断是否认证过
		if (!currentUser.isAuthenticated()) {
			//没认证,创建令牌(携带账号密码)
			UsernamePasswordToken token = 
					new UsernamePasswordToken(user.getAccount(), user.getPwd());
			//认证
			try {
				currentUser.login(token);
				//不报异常
				System.out.println("认证成功!");
				
			} catch (UnknownAccountException e) {
				result.setMessage("账号不存在");
				result.setStatus(Result.ERROR);
				System.out.println("账号不存在");
				
			} catch (IncorrectCredentialsException e){
				result.setMessage("密码错误");
				result.setStatus(Result.ERROR);
				System.out.println("密码错误");
				
			} catch (Exception e) {
				System.out.println("其它异常");
			}	
		}
//		User user1 = userService.findUserByAccount(user.getAccount());
		
		
//		HttpSession session = request.getSession();

//		if (user1!=null && user.getPwd().equals(user1.getPwd())) {
//			
//			//设置session的过期时间
//			session.setMaxInactiveInterval(1*60*60*24*30);//单位秒
//			//存放信息  记录用户登录
//			session.setAttribute("uid", user1.getUid());
//			result.setStatus(Result.SUCCESS);
//		}else{
//			result.setStatus(Result.ERROR);
//			result.setMessage("账号或者密码有误!");
//		}
		
		 result.setStatus(Result.SUCCESS);
		
		return result;
	}
//	@RequestMapping("/account")
//	@ResponseBody
//	public Result<Object> account(HttpServletRequest request){
//		Result<Object> result = new Result<>();
//		if(request.getSession().getAttribute("uid")==null){
//		result.setStatus(Result.ERROR);
//		
//	}else{
//		result.setStatus(Result.SUCCESS);
//	}
//		return result;
//		
//	}
	@RequestMapping("/all")
	@ResponseBody
	public Result<List<User1>> all(){
		Result<List<User1>> result = new Result<>();
		result.setData( userService.all());
		return result;
		
	}
	@RequestMapping("/addRoles")
	@ResponseBody
	public Result<Object> addRole1(int uid, int roles[]){
		System.out.println(Arrays.toString(roles));
		Result<Object> result = new Result<>();
		int c=0;
		

		
		for (int i = 0; i < roles.length; i++) {
			int j = roles[i];
			 userService.addUser1AndRole1(uid, j);
			 c++;
		}
		if(c>0 ) {
			result.setMessage("ok");
			result.setStatus(Result.SUCCESS);
		}else {
			result.setMessage("ok");
			result.setStatus(Result.ERROR);
		}
		return result;
		
	}
	@RequestMapping("/addPerms")
	@ResponseBody
	public Result<Object> addRole1AndPerms1(int rid,int[] perms){
		System.out.println(Arrays.toString(perms));
		Result<Object> result = new Result<>();
		int c=0;
		

		
		for (int i = 0; i < perms.length; i++) {
			int j = perms[i];
			 userService.addRole1AndPerms1(rid, j);
			 c++;
		}
		if(c>0 ) {
			result.setMessage("ok");
			result.setStatus(Result.SUCCESS);
		}else {
			result.setMessage("ok");
			result.setStatus(Result.ERROR);
		}
		return result;
	
		
	}
	
	@RequestMapping("/logout1")
	@ResponseBody
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "redirect:../index.html";
		
	}
	
//	@RequestMapping("/upload")
//	@ResponseBody
//	//如果前端提交的key与参数的名字不一致则可以通过@RequestParam获取指定key的值
//	public Result<Object> uploadImage(@RequestParam("headImg")MultipartFile image,HttpServletRequest request) throws IllegalStateException, IOException{
//		//获取原始文件名
//		String fileName = image.getOriginalFilename();
//		System.out.println(image);
//		//获取文件存放路径
//		String path = request.getServletContext().getRealPath("");//项目根路径
//		File app = new File(path);
//		//
//		File webapps = app.getParentFile();
//		//
//		File mvc = new File(webapps, "mvc");
//		//
//		if (!mvc.exists()) {
//			mvc.mkdirs();
//		}
//		//UUID
//		//处理文件名   生成一个随机字符串  32位
//		String radom_name = UUID.randomUUID().toString();
//		//得到文件后缀名
//		String sufix = fileName.substring(fileName.lastIndexOf("."),fileName.length());
//		//
//		fileName = radom_name+sufix;
//		
//		File newFile = new File(mvc,fileName);
//		//保存文件
//		image.transferTo(newFile);
//		 int userid = (int)request.getSession().getAttribute("uid");
//		 //
//			User1 user = new User1().setUid(userid).setImage(fileName);
//			//
//			
//			user1Service.updateHeadImg(user);
//			
//			//返回结果
//			Result<Object> result = new Result<>();
//			result.setData("/mvc/"+fileName);
//		 return result;
//	}
	
//	@RequestMapping("/center")
//	public ModelAndView center(HttpServletRequest request){
//		ModelAndView modelAndView = new ModelAndView();
//		
//		User1 user = new User1().setAccount("admin");
//		//向modeladnview中放数据其实就是向request中放数据
//		modelAndView.addObject("user", user);
//		modelAndView.addObject("isVip", true);
//		request.setAttribute("gender", "man");
//		
//		//session中的拿不到
//		request.getSession().setAttribute("age", "10");
//		
//		//
//		List<String> menus = Arrays.asList("添加课程","在线考试","视频上传","视频管理");
//		modelAndView.addObject("menus", menus);
//		
//		modelAndView.setViewName("/center.html");
//		
//		return modelAndView;
//	}
	/*
	 * springboot项目有几个默认可以放静态文件的地方
	 * static
	 * templates
	 * public
	 * WEB-INF/webapp      a.html    /a.html
	 * 
	 */

}






